﻿using DTcms.Common;
using System.Web.Mvc;

namespace DTcms.Web.Mvc.UI.Page
{
    public partial class article_show : BaseController
    {
        protected int id;
        protected string call_index = string.Empty;
        protected string channel_name = string.Empty;
        protected string redirect = string.Empty;//回传地址
        protected Model.article article = new Model.article();

        /// <summary>
        /// OnInit事件,让频道名称变量先赋值
        /// </summary>
        public article_show(Model.site_channel site_model)
        {
            id = DTRequest.GetQueryInt("id");
            call_index = Utils.SafeXXS(DTRequest.GetQueryString("call_index"));
            channel_name = site_model.name;

            #region 验证频道列表数据
            BLL.article bll = new BLL.article();
            if (id > 0) //如果ID获取到，将使用ID
            {
                if (!bll.ArticleExists(channel_name, id))
                {
                    redirect = linkurl("error", "?msg=" + Utils.UrlEncode("内容不存在！"));
                    return;
                }
                article = bll.ArticleModel(channel_name, id);
            }
            else if (!string.IsNullOrEmpty(call_index)) //否则检查设置的别名
            {
                if (!bll.ArticleExists(channel_name, call_index))
                {
                    redirect = linkurl("error", "?msg=" + Utils.UrlEncode("内容不存在！"));
                    return;
                }
                article = bll.ArticleModel(channel_name, call_index);
                //赋值文章ID
                id = article.id;
            }
            else
            {
                redirect = linkurl("error", "?msg=" + Utils.UrlEncode("无关键参数效验！"));
                return;
            }
            if (article.status == 1)
            {
                redirect = linkurl("error", "?msg=" + Utils.UrlEncode("浏览得该信息审核还未通过！"));
                return;
            }
            //判断是否是链接
            if (!string.IsNullOrEmpty(article.link_url))
            {
                redirect = article.link_url;
                return;
            }
            //判断SEO标题
            if (string.IsNullOrEmpty(article.seo_title))
            {
                article.seo_title = site.seo_title;
            }
            if (string.IsNullOrEmpty(article.seo_keywords))
            {
                article.seo_keywords = site.seo_keywords;
            }
            if (string.IsNullOrEmpty(article.seo_description))
            {
                if (string.IsNullOrEmpty(article.zhaiyao))
                {
                    article.seo_description = Utils.DropHTML(article.content, 255);
                }
                else
                {
                    article.seo_description = article.zhaiyao;
                }
                if (string.IsNullOrEmpty(article.seo_description))
                {
                    article.seo_description = site.seo_description;
                }
            }
            
            bll.UpdateField(site_model.id, id, "click=click+1");//阅读次数
            #endregion
        }
    }
}
